#include "wizard.h"
#include "planche.h"
#include <QLabel>
#include <QMessageBox>
#include <QDebug>
#include "mainwindow.h"

Wizard::Wizard(MainWindow *parent) :
    QWizard(parent),CollageWindow()
{
    this->mainWindow=parent;

    WizardPageBegin *wizardPageBegin = new WizardPageBegin(this);
    WizardPagePhotos *wizardPagePhotos = new WizardPagePhotos(this);
    WizardPageForme* wizardPageForme = new WizardPageForme(this);
    WizardPageExtra* wizardPageExtra = new WizardPageExtra(this);
    WizardPageParametre* wizardPageParam = new WizardPageParametre(this);

    setPage(PAGE_INTRO,wizardPageBegin);
    setPage(PAGE_PHOTOS,wizardPagePhotos);
    setPage(PAGE_FORME,wizardPageForme);
    setPage(PAGE_EXTRA,wizardPageExtra);
    setPage(PAGE_PARAM,wizardPageParam);


    setWindowTitle("Aide à la création");
    setWizardStyle(ModernStyle);
    setFixedWidth(600);


}
void Wizard::on_update_delete()
{
    CollageWindow::on_update_delete();
    //déverouillage du bouton next lorque l'on drop des images dans la list
    currentPage()->completeChanged();

}

void Wizard::showMessage(QString s)
{

}

Wizard::~Wizard()
{

}

WizardPageBegin::WizardPageBegin(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::WizardPageBegin)
{
    ui->setupUi(this);

    setPixmap(Wizard::WatermarkPixmap,QPixmap(":images/watermark"));
}

WizardPageBegin::~WizardPageBegin()
{
    delete ui;
}

WizardPageExtra::WizardPageExtra(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::WizardPageExtra)
{
    ui->setupUi(this);

    planche = new Planche(ui->frame->width()-20,ui->frame->height()-20,ui->frame);
}

bool WizardPageExtra::validatePage ()
{
    Wizard* wi = (Wizard*)this->wizard();

    if(planche->polygon->count()<3)
    {
        QMessageBox::information( this, "Forme non complète",
                                     "Votre forme doit contenir au moin 3 sommets",
                                     QMessageBox::Ok);
        return false;
    }
    else
    {
        //forme OK , on recopie le polygon de la planche dans l'attribut wizard

        wi->polygon = new QPolygon(*planche->polygon);
        return true;
    }

}

WizardPageExtra::~WizardPageExtra()
{
    delete planche;
    delete ui;
}

WizardPagePhotos::WizardPagePhotos(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::WizardPagePhotos)
{
    ui->setupUi(this);
    Wizard* wi = (Wizard*)this->parent();
    wi->listPhotos = new MyListPhotos(wi,440,250,ui->groupBox);
}

WizardPagePhotos::~WizardPagePhotos()
{
    delete ui;
}


bool WizardPagePhotos::isComplete()const
{
    Wizard* wi = (Wizard*)this->wizard();
    return wi->listPhotos->count()>0;
}

void WizardPagePhotos::on_retireButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->supprimePhoto();
}

void WizardPagePhotos::on_clearButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->supprimeLesPhotos();
}


void WizardPagePhotos::on_ajoutButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->ajoutPhotoParDialog(wi);
}

WizardPageForme::WizardPageForme(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::WizardPageForme)
{
    ui->setupUi(this);


}

void WizardPageForme::initializePage()
{
    on_rectangleRadioButton_clicked();
}

WizardPageForme::~WizardPageForme()
{
    delete ui;
}

int WizardPageForme::nextId() const
 {
     if (ui->extraRadioButton->isChecked()) {
         return Wizard::PAGE_EXTRA;
     } else {
         return Wizard::PAGE_PARAM;
     }
 }


void WizardPageForme::on_cercleRadioButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->SetCercle();
    QPixmap* pixmap = wi->GetPolygonePixmap(wi->polygon);
    QPixmap pix2 = pixmap->scaled(ui->labelApercu->size(), Qt::KeepAspectRatio,Qt::SmoothTransformation);
    ui->labelApercu->setPixmap(pix2);
    wi->params->setForme(Cercle);
}

void WizardPageForme::on_rectangleRadioButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->SetRectangle();
    QPixmap* pixmap = wi->GetPolygonePixmap(wi->polygon);
    QPixmap pix2 = pixmap->scaled(ui->labelApercu->size(), Qt::KeepAspectRatio,Qt::SmoothTransformation);
    ui->labelApercu->setPixmap(pix2);
    wi->params->setForme(Rectangle);
}

void WizardPageForme::on_extraRadioButton_clicked()
{
    Wizard* wi = (Wizard*)this->wizard();
    wi->params->setForme(Extra);
}

WizardPageParametre::WizardPageParametre(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::WizardPageParametre)
{
    ui->setupUi(this);
}

WizardPageParametre::~WizardPageParametre()
{
    delete ui;
}

bool WizardPageParametre::validatePage ()
{
    Wizard* wi = (Wizard*)this->wizard();

    /*Suppression des photos du projet courant */
    wi->mainWindow->supprimeLesPhotos();


    /*Ajout des photos au projet principal*/
    int i=0;
    while(i<wi->listPhotos->count())
    {
        wi->mainWindow->addPhoto(wi->listPhotos->item(i)->data(Qt::UserRole).toString());
        i++;
    }

    wi->mainWindow->on_update_delete();

    /*Modification de la forme courante */

    switch(wi->params->getForme())
    {
        case Cercle:
            wi->mainWindow->on_cercle_radioButton_clicked();
        break;
        case Rectangle:
            wi->mainWindow->on_rectangle_radioButton_clicked();
        break;
        case Extra:
            wi->mainWindow->polygon=new QPolygon(*wi->polygon);
            //on récupère la pixmap de la planche à dessin pour l'aperçu
            WizardPageExtra* extra =  (WizardPageExtra*)wi->page(wi->PAGE_EXTRA);
            wi->mainWindow->setApercu(extra->planche->pixmap);
            wi->mainWindow->checkExtra(true);
        break;
    }

    if(ui->mode->isChecked())
    {
        wi->mainWindow->params->setAutoParam(pictureSizeId);
        wi->mainWindow->params->setResultSize(QSize(800,600));
        wi->mainWindow->params->setAddAllPicture(true);
        wi->mainWindow->params->setColorBordure(Qt::black);
        wi->mainWindow->params->setPictureDistance(25);
    }
    else if(ui->mode_2->isChecked())
    {
        wi->mainWindow->params->setAutoParam(pictureQuantityId);
        wi->mainWindow->params->setResultSize(QSize(800,600));
        wi->mainWindow->params->setAddAllPicture(false);
        wi->mainWindow->params->setPictureSize(100);
        wi->mainWindow->params->setColorBordure(Qt::black);
        wi->mainWindow->params->setPictureDistance(25);
    }
    else if(ui->mode_3->isChecked())
    {
        wi->mainWindow->params->setAutoParam(pictureDistanceId);
        wi->mainWindow->params->setResultSize(QSize(800,600));
        wi->mainWindow->params->setAddAllPicture(true);
        wi->mainWindow->params->setPictureSize(100);
        wi->mainWindow->params->setColorBordure(Qt::black);
    }

    wi->mainWindow->refreshParams(all,true,true);

    return true;

}



